package com.lsfb.testgamedemo;

import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.RemoteViews;

import com.lsfb.testgamedemo.Utils.LittleUtils;
import com.lsfb.testgamedemo.Utils.SPUtils;
import com.lsfbandroid.helloplugin.R;

import java.util.HashMap;
import java.util.List;


public class MyService extends AccessibilityService {
    private String TAG = "nonsense";
    private String pwds = "123";
    private String LastClass = "123";

    private String Tname = "";
    private String saveState = "123";//保存当前的窗口名字
    private int mode = -1; //模式; -1进入房间不做操作(恢复常态);0:默认循环模式：不停的进入退出房间; 1 改房间名

    private String Rename_name = "123";
    private String Labainput = "123";

    private String Tnum = "1";
    private String Bnum = "1'";
    private String SMid = "123";
    private String FengId = "1";

    private boolean RunOrder = false;
    private String Pass_lock = "123";


    private String[] OrderStringArr = {"开始", "房名", "锁房", "暂停", "解锁", "清公屏", "开公屏", "关公屏", "禁", "解禁", "踢", "抱", "抱树", "封", "解封", "喇叭", "抱踢", "踢树", "速度"};

    private String tmpdata = "123";
    private String BanNum = "1";
    private boolean FirstSetState = false;
    private boolean autoRefresh = false; //是否自动刷房
    // code //命令
    private HashMap<String, String> orderlist = new HashMap<String, String>();


    //识别的文字保存
    private String Room = "房间";
    private String EnterRoom = "进入我的房间";
    private String ExitRoom = "退出房间";
    private String NotifyRoomName = "修改房间名";
    private String LookRoom = "房间加锁";
    private String unLookRoom = "房间解锁";
    private String CloseChat = "关闭公屏";
    private String Gping = "公屏";
    private String OpenChat = "打开公屏";
    private String Exit = "退出";
    private String BanTalk = "禁麦";
    private String unBanTalk = "解除禁麦";
    private String Tman = "踢出房间";
    private String BaoMan = "设为旁听";
    private String Baoshu = "封闭此座位";
    private String ShangMai = "抱用户上麦";
    private String Jiefeng = "解封此座位";


    //特殊控件保存
    private String TextViewName = "android.widget.TextView";
    private String LinLayout = "android.widget.LinearLayout";
    private String List = "android.widget.ListView";
    private String ViewPagerName = "android.support.v4.view.ViewPager";
    private String HorizontalScrollView = "android.widget.HorizontalScrollView";


    //类名保存
    private String page1 = "com.yy.huanju.MainActivity";
    private String page_room = "com.yy.huanju.chatroom.ChatroomActivity";
    private String page_room_exit_menu = "com.yy.huanju.widget.dialog.d";
    private String page_room_serach_dialog = "com.yy.huanju.widget.dialog.b";
    private String page_room_exit_menu_exitdialog = "android.app.AlertDialog";
    private String page_rename_room = "com.yy.huanju.chatroom.internal.ChatRoomModifyNameActivity";
    private String page_lock_room = "com.yy.huanju.chatroom.internal.ChatRoomLockActivity";
    private String Progressdialg = "android.app.ProgressDialog";
    private String Baosheishangmai = "com.yy.huanju.chatroom.YGroupMemberActivity";

    //ID保存
    private String ExitBtnId = "com.yy.huanju:id/topbar_right_child_layout";
    private String ExitDialgoRightBtn = "android:id/button1";
    private String ExitDialgoleftBtn = "android:id/button2";
    private String Edittext_rename = "com.yy.huanju:id/room_name";
    private String Btn_PostRename = "com.yy.huanju:id/btn_modify";
    private String edit_RoomPass = "com.yy.huanju:id/room_pass";
    private String Btn_LoockRoom = "com.yy.huanju:id/btn_lock";
    private String Grid_chatroom = "com.yy.huanju:id/chatroom_gridview";
    private String Btn_SearchUser = "com.yy.huanju:id/layout_search"; //搜索按钮
    private String Edit_SearchUser = "com.yy.huanju:id/tv_alert_message"; //ID编辑框
    private String Btn_BaoUser = "com.yy.huanju:id/btn_positive"; //抱她上麦
    private String Btn_saveLaba = "com.yy.huanju:id/btn_topic_save"; //保存喇叭
    private String edit_laba = "com.yy.huanju:id/edit_text_topic"; //喇叭文字
    private String btn_edit_laba = "com.yy.huanju:id/layout_display_topic"; //编辑喇叭
    private String btn_enter_top = "com.yy.huanju:id/topbar_center_child_layout"; //进入在线列表
    private String btn_kicout = "com.yy.huanju:id/btn_kickout"; //列表图标踢

    private Long Timedelay = 1000l;


    @Override
    public void onCreate() {

        super.onCreate();
        NotificationManager manager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        PendingIntent pendingintent = PendingIntent.getActivity(this, 0,
                new Intent(this, App.class), 0);
        Notification.Builder builder = new Notification.Builder(this);//新建Notification.Builder对象
        builder.setContentTitle("Hello助手");//设置标题
        builder.setContentText("正在运行");//设置内容
        builder.setSmallIcon(R.mipmap.ic_launcher);//设置图片
        builder.setContentIntent(pendingintent);//执行intent
        Notification notification = builder.getNotification();//将builder对象转换为普通的notification
        startForeground(1, notification);//让 MyService 变成一个前台服务，并在系统状态栏中显示出来。
        manager.notify(1, notification);//运行notification

    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "开启服务");
        flags = START_FLAG_REDELIVERY;
        return super.onStartCommand(intent, flags, startId);
    }

    public void GetSpeed() {
        String sped = (String) SPUtils.get(getApplicationContext(), "speed", "2");
        switch (Integer.valueOf(sped)) {
            case 1:
                Timedelay = 200l;
                break;
            case 2:
                Timedelay = 400l;
                break;
            case 3:
                Timedelay = 600l;
                break;
        }
    }

    @Override
    public void onAccessibilityEvent(AccessibilityEvent event) {
//        Log.e(TAG, event.getPackageName().toString());
        int eventType = event.getEventType();

        GetSpeed();
        AccessibilityServiceInfo info = getServiceInfo();
        info.notificationTimeout = Timedelay;
        setServiceInfo(info);
//        Log.e(TAG, "接收的事件类型:" + String.valueOf(eventType));
        if (!App.Islogin) {
            return;
        }
        switch (eventType) {
            case AccessibilityEvent.TYPE_NOTIFICATION_STATE_CHANGED:  //监听消息
//                Log.e(TAG, "接受到消息窗体改变;" + event.getClassName());
//                pause = true;
//                Log.e(TAG, "暂停接受窗口事件");
                GetPwd();//每次获取一次密码
                List<CharSequence> texts = event.getText();
                if (!texts.isEmpty()) {
                    for (CharSequence text : texts) {
                        String content = text.toString();
                        Log.e(TAG, "接收的消息内容:" + content);
                        String[] contents = content.split(":");
                        if (contents.length > 1) {
                            int tmpmode = dealOrder(contents[1].replaceAll(" ", ""));
                            Log.e(TAG, "获取的命令代码:" + tmpmode);
                            if (tmpmode == 18)  //调整刷房速度
                            {
                                Log.e(TAG, "刷房速度改变");
                                String speed = tmpdata;
                                switch (Integer.valueOf(speed)) {
                                    case 1:
                                        SPUtils.put(getApplicationContext(), "speed", "1");
                                        break;
                                    case 2:
                                        SPUtils.put(getApplicationContext(), "speed", "2");
                                        break;
                                    case 3:
                                        SPUtils.put(getApplicationContext(), "speed", "3");
                                        break;
                                }
                            } else {
                                if (tmpmode != -1) { //判断是否是个命令
                                    if (tmpmode > 0) {
                                        RunOrder = true;
                                        if (tmpmode == 3) {
                                            autoRefresh = false;//在获取暂停命令后停止容错线程
                                        }
                                    } else if (tmpmode == 0) {
                                        RunOrder = false;
                                        autoRefresh = true;//在获取开始刷房后开启容错线程
                                    }
                                    mode = tmpmode;
                                    Log.e(TAG, "开始执行指令");
                                    FirstSetState = false;
                                    if (mode == -1) {
                                        checkMode(saveState);
                                    }
                                }
                            }
                        }
                    }
                }
                break;
            case AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED:
                String className = event.getClassName().toString();
                //待加入 延迟时间
                Log.e(TAG, "改变窗口名称:" + className);
//                if (!errorthread.isAlive() && autoRefresh) {
//                    errorthread.start();
//                }
                LastClass = saveState;
                saveState = className;
//                recordCompat = AccessibilityEventCompat.asRecord(event);

//                if (!RunOrder) {
//                    Log.e(TAG, "接受窗口事件.......");
                checkMode(saveState);
//                }
                break;
            case AccessibilityEvent.TYPE_VIEW_CLICKED:
                String className1 = event.getClassName().toString();
                Log.e(TAG, "点击的窗口:" + event.getText());
                break;
//            case AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED:
//                String className1 = event.getClassName().toString();
//                Log.e(TAG, "classname1:" + className1 + "text" + event.getText());
//                break;
//            case AccessibilityEvent.TYPE_VIEW_TEXT_SELECTION_CHANGED:
//                String className2 = event.getClassName().toString();
//                Log.e(TAG, "classname2:" + className2 + "text" + event.getText());
//                break;
        }
    }


    @Override

    public void onInterrupt() {
        RunOrder = false;
        FirstSetState = false;
        mode = -1;
        autoRefresh = false;
        Log.e(TAG, "服务停止中");
    }


    @Override
    protected void onServiceConnected() {
        Log.e(TAG, "监听服务连接");
        AccessibilityServiceInfo info = new AccessibilityServiceInfo();
        info.eventTypes = AccessibilityEvent.TYPES_ALL_MASK;
        info.feedbackType = AccessibilityServiceInfo.FEEDBACK_SPOKEN;
        info.notificationTimeout = 200;
        setServiceInfo(info);
        info.packageNames = new String[]{"com.yy.huanju"};
        setServiceInfo(info);
        super.onServiceConnected();
        errorthread.start();
    }

    @Override
    public void onDestroy() {
        Log.e(TAG, "服务停止");
        stopForeground(true);
        super.onDestroy();
    }

    private String errorClass = null;
    private int savesecond = 0;
    private int time = 8;
    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);
            switch (msg.what) {
                case 1:
                    initretdata();
                    checkMode(saveState);
                    break;
            }
        }
    };

    private Thread errorthread = new Thread(new Runnable() {
        @Override
        public void run() {
            while (true) {
                time = (int) (1L * Timedelay / 100l) + 30;
                if (autoRefresh) {
//                    Log.e(TAG, "容错线程运行中:" + savesecond);
                    if (errorClass == null) {
                        errorClass = saveState;
                    } else {
                        if (errorClass.equals(saveState)) {
                            savesecond++;
                            if (savesecond == (time + 1)) {
                                //reset 重置命令
                                Log.e(TAG, "检测到错误，恢复初始状态");
                                savesecond = 0;
                                handler.sendEmptyMessage(1);
                            }
                        } else {
                            savesecond = 0;
                            errorClass = saveState;
                        }
                    }
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    });

    private void checkMode(String classname) {
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
        Log.e(TAG, "实时窗口名:" + saveState);
        switch (mode) {
            case -2:
                if (FirstSetState) {
                    reSetWindows(saveState);
                }
                break;
            case -1: //待命状态
                Log.e(TAG, "停止任何动作");
                break;
            case 0:
                //默认循环操作
                Log.e(TAG, "开启循环服务");
                if (!RunOrder) {
                    loopDoExit(saveState);
                }
                break;
            case 1:
                Log.e(TAG, "改");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                Rename_name = tmpdata;
                ReNameRoom(saveState);
                break;
            case 2:
                Log.e(TAG, "锁房");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                Pass_lock = LittleUtils.GetNumString(tmpdata); //有隐患。后面再说
                LooKRoom(saveState);
                break;
            case 3:
                Log.e(TAG, "暂停");
                mode = -1;
                break;
            case 4:
                Log.e(TAG, "解锁");
                UnLock(saveState);
                break;
            case 5:
                Log.e(TAG, "清公屏");
                ClearGping(saveState);
                break;
            case 6:
                Log.e(TAG, "开公屏");
                OpenGping(saveState);
                break;
            case 7:
                Log.e(TAG, "关公屏");
                CloseGping(saveState);
                break;
            case 8:
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                BanNum = LittleUtils.GetNumString(tmpdata);
                Log.e(TAG, "禁");
                BanTalk(saveState);
                break;
            case 9:
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                BanNum = LittleUtils.GetNumString(tmpdata);
                Log.e(TAG, "解禁");
                unBanTalk(saveState);
                break;
            case 10:
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                Tnum = LittleUtils.GetNumString(tmpdata);
                Log.e(TAG, "T人");
                Tnum(saveState);
                break;
            case 11:
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                Bnum = LittleUtils.GetNumString(tmpdata);
                Log.e(TAG, "抱人下麦");
                Baonum(saveState);
                break;
            case 12:
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                String tmp = LittleUtils.GetNumString(tmpdata);
                SMid = tmp.substring(1, tmp.length());
                Tnum = tmp.substring(0, 1);
                Log.e(TAG, "抱树ID：" + SMid);
                Log.e(TAG, "抱树上麦");
                BaoShushangMai(saveState);
                break;
            case 13:
                Log.e(TAG, "封闭座位");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                FengId = LittleUtils.GetNumString(tmpdata);
                Feng(saveState);
                break;
            case 14:
                Log.e(TAG, "解除封闭座位");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                FengId = LittleUtils.GetNumString(tmpdata);
                Unfeng(saveState);
                break;
            case 15:
                Log.e(TAG, "换喇叭");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                Labainput = tmpdata;
                EditLaba(saveState);
                break;
            case 16:
                Log.e(TAG, "抱后踢掉");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                String tmp1 = LittleUtils.GetNumString(tmpdata);
                SMid = tmp1.substring(1, tmp1.length());
                Tnum = tmp1.substring(0, 1);
                BaoTi(saveState);
                break;
            case 17:
                Log.e(TAG, "踢树");
                if (tmpdata.isEmpty() || tmpdata.length() == 0) {
                    return;
                }
                Tname = tmpdata;
                GetListT(saveState);
                break;
        }

//        },Timedelay);

//        isCheck = false;
    }

    /**
     * 循环做退出操作
     */
    private void loopDoExit(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page1) && !LastClass.equals(Progressdialg)) {
                MainPage2MyRoom(nodeInfo);
            } else if (saveState.equals(page_room) && !LastClass.equals(page_room_exit_menu_exitdialog)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, ExitRoom)) {
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            } else if (saveState.equals(page_room_exit_menu_exitdialog)) {
                if (!ClickIdBtn(ExitDialgoRightBtn, nodeInfo)) {
                    Log.e(TAG, "按了返回");
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);//返回物理按键
                }
            }
        } else {
            reSetWindows(saveState);
        }
//        checkMode(saveState);
//        nodeInfo.recycle();
    }


    /**
     * 验证命令
     *
     * @param order
     * @param old
     * @return
     */
    private boolean veryOrder(String order, String old) {


        String tmp = order.replaceFirst(old, ",");
        String[] tmps = tmp.split(",");
        if (tmps != null && tmps.length != 0) {
            if (tmps[0].equals(pwds)) {
                if (tmps.length > 1) {
                    tmpdata = tmps[1];//存储临时数据，然后复制给需要添加的数据
                    Log.e(TAG, "临时数据：" + tmpdata);
                }
                return true;
            }
        }
        return false;
    }


    /**
     * 处理命令
     */
    private int dealOrder(String order) {
        int i = 0;
        int j = 0;
        boolean isvery = false;
        for (String s : OrderStringArr) {

            if (veryOrder(order, s)) {
                isvery = true;
                j = i;
//                break;
            }
            i++;
        }
        if (isvery) {
            return j;
        }
        return -1;
    }

    /**
     * 主页跳转我的房间
     *
     * @param nodeInfo
     */
    public void MainPage2MyRoom(final AccessibilityNodeInfo nodeInfo) {
        try {
            List<AccessibilityNodeInfo> list2 = nodeInfo.findAccessibilityNodeInfosByText("进入我的房间");
            for (AccessibilityNodeInfo nodeInfo1 : list2) {

                if (nodeInfo1.getText().toString().equals(EnterRoom) && nodeInfo1.getParent().getClassName().equals(LinLayout) && nodeInfo1.getParent().isClickable()) {
                    if (nodeInfo1 != null && nodeInfo1.getParent() != null && nodeInfo1.getParent().getParent() != null && nodeInfo1.getParent().getParent().getParent() != null) {
                        //判断是首页并且点击房间标签;
                        Log.e(TAG, "点击进入我的房间");
                        nodeInfo1.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                    }
                }
            }
        } catch (Exception e) {
            Log.e(TAG, "点击进入我的房间出现错误:"+e);
        }
    }

    /**
     * 点击右上角退出按钮弹出框
     *
     * @param nodeInfo
     */
    private void ClickRightBtn(AccessibilityNodeInfo nodeInfo) {
        try {
            List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId(ExitBtnId);
            if (list.size() != 0) {
                list.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
            }
        } catch (Exception e) {
            Log.e(TAG, "err" + e);
            return;
        }


    }

    /**
     * 退出弹出的POP框点击按钮列表
     *
     * @param nodeInfo
     * @param buttonname
     */
    private boolean ClickPopExitButton(AccessibilityNodeInfo nodeInfo, String buttonname) {
        List<AccessibilityNodeInfo> list2 = null;
        try {
            list2 = nodeInfo.findAccessibilityNodeInfosByText(buttonname);
        } catch (Exception e) {
            Log.e(TAG, "错误信息:未找到按钮:" + e);
            return false;
        }
        if (!list2.isEmpty()) {
            for (AccessibilityNodeInfo nodeInfo1 : list2) {
                if (nodeInfo1 != null && nodeInfo1.getParent() != null) {
                    if (nodeInfo1.getText().toString().equals(buttonname) && nodeInfo1.getClassName().equals(TextViewName) && nodeInfo1.getParent().getClassName().equals(LinLayout)) {
                        nodeInfo1.getParent().performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        return true;
                    }
                }
            }
        }
        return false;
    }

    /**
     * 恢复到我的房间内
     *
     * @param classname
     */
    private void reSetWindows(String classname) {
        AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
        if (saveState.equals(page_room) && !LastClass.equals(page_room_exit_menu_exitdialog)) {
            FirstSetState = true;
            Log.e(TAG, "回到聊天室");
            checkMode(saveState);
        } else if (saveState.equals(page1) && !LastClass.equals(Progressdialg)) {
            //如果在首页
            MainPage2MyRoom(nodeInfo);
        } else if (saveState.equals(page_room_exit_menu_exitdialog)) {
            ClickIdBtn(ExitDialgoleftBtn, nodeInfo);
        } else if (!saveState.equals(Progressdialg)) {
            Log.e(TAG, "按了返回");
            performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);//返回物理按键

        }
    }

    /**
     * 修改房间名
     */
    private void ReNameRoom(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                ClickPopExitButton(nodeInfo, NotifyRoomName);
            } else if (saveState.equals(page_rename_room)) {
                rename(nodeInfo);
//                WriteText(nodeInfo,Edittext_rename);
                initretdata();
            }
        } else {
            reSetWindows(saveState);
        }
//        nodeInfo.recycle();
//        checkMode(saveState);
    }

    /**
     * 修改操作
     *
     * @param nodeInfo
     */
    private void rename(AccessibilityNodeInfo nodeInfo) {
        List<AccessibilityNodeInfo> list = null;
        try {
            list = nodeInfo.findAccessibilityNodeInfosByViewId(Edittext_rename);
        } catch (Exception e) {
            return;
        }
        if (list == null) {
            return;
        }
        ClipboardManager clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        AccessibilityNodeInfo nodeInfo1 = list.get(0);
        Bundle arguments = new Bundle();
        arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE);
        arguments.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
                true);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
                arguments);
        ClipData clip = ClipData.newPlainText("111", Rename_name);
        clipboardManager.setPrimaryClip(clip);
        if (!clipboardManager.getPrimaryClip().getItemAt(0).getText().toString().equals(Rename_name))
        {
            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        }
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        ClickIdBtn(Btn_PostRename, nodeInfo);//修改失败会直接退出
        initretdata();
    }

    /**
     * 重置数据（自检然后进行循环）
     */
    private void initretdata() {
        Log.e("end", "初始化数据准备进入循环");
        RunOrder = false;
        FirstSetState = false;
        mode = 0;
    }

    /**
     * 点击按钮(通过ID查找的)
     */
    public boolean ClickIdBtn(String BtnID, AccessibilityNodeInfo nodeInfo) {
        List<AccessibilityNodeInfo> list = null;
        try {
            list = nodeInfo.findAccessibilityNodeInfosByViewId(BtnID);
        } catch (Exception e) {
            return false;
        }
        if (list == null || list.isEmpty()) {
            return false;
        }
        AccessibilityNodeInfo nodeInfo1 = list.get(0);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_CLICK);
        return true;
    }


    /**
     * 锁房
     */
    private void LooKRoom(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, LookRoom)) {
                    ClickPopExitButton(nodeInfo, unLookRoom);
                }

            } else if (saveState.equals(page_lock_room)) {
                LockPass(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu_exitdialog)) {
                ClickIdBtn(ExitDialgoRightBtn, nodeInfo);
            }
        } else {
            reSetWindows(saveState);
        }

    }

    /**
     * 锁房加密操作
     *
     * @param nodeInfo
     */
    private void LockPass(AccessibilityNodeInfo nodeInfo) {
//        作者：俞俞俞其荣
//        链接：https://www.zhihu.com/question/43473655/answer/131252436
//        来源：知乎
//        著作权归作者所有，转载请联系作者获得授权。

//        ndroid 5.0 版本及以上：

        List<AccessibilityNodeInfo> list = null;
        try {
            list = nodeInfo.findAccessibilityNodeInfosByViewId(edit_RoomPass);
        } catch (Exception e) {
            return;
        }
        if (list == null) {
            return;
        }

        AccessibilityNodeInfo nodeInfo1 = list.get(0);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Bundle arguments = new Bundle();
//            arguments.putCharSequence(
//                    AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, Pass_lock);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { //        Android 4.3 版本及以上：
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_CUT);
//            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//            ClipData clip = ClipData.newPlainText(TAG, Pass_lock);
//            clipboard.setPrimaryClip(clip);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
//        }
        Bundle arguments = new Bundle();
        arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE);
        arguments.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
                true);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
                arguments);
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("321", Pass_lock);
        clipboard.setPrimaryClip(clip);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);

        ClickIdBtn(Btn_LoockRoom, nodeInfo);//修改失败会直接退出进入循环
        initretdata();
    }

    /**
     * 解锁房间
     */
    private void UnLock(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, unLookRoom)) {
                    //解锁失败
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);//返回物理按键
                }

            } else if (saveState.equals(page_room_exit_menu_exitdialog)) {
                List<AccessibilityNodeInfo> list2 = nodeInfo.findAccessibilityNodeInfosByViewId(ExitDialgoRightBtn);
                list2.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                initretdata();
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 先开后关
     */
    private void ClearGping(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, CloseChat)) {
                    ClickPopExitButton(nodeInfo, OpenChat);
                    initretdata();
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 开公屏
     */
    private void OpenGping(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                if (ClickPopExitButton(nodeInfo, OpenChat)) {
                    initretdata();
                } else {
                    Log.e(TAG, "已经开屏");
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 关公屏
     */

    private void CloseGping(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                ClickRightBtn(nodeInfo);
            } else if (saveState.equals(page_room_exit_menu)) {
                if (ClickPopExitButton(nodeInfo, CloseChat)) {
                    initretdata();
                } else {
                    Log.e(TAG, "已经关屏");
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 静麦
     */
    private void BanTalk(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(BanNum), nodeInfo)) {
//                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (ClickPopExitButton(nodeInfo, BanTalk)) {
                    initretdata(); //禁麦成功
                } else {
                    //禁麦失败，这个位置没有人 或者这个麦已经被禁了
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 解除静麦
     */
    private void unBanTalk(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(BanNum), nodeInfo)) {
//                    initretdata();
//                    checkMode(saveState);
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (ClickPopExitButton(nodeInfo, unBanTalk)) {
                    initretdata(); //禁麦成功
//                        mode = -1;
                } else {
                    //禁麦失败，这个位置没有人 或者已经被解除禁麦了
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 点击num顺序的人
     *
     * @param num
     */
    private boolean ClickNum(int num, AccessibilityNodeInfo nodeInfo) {
        try {
            List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId(Grid_chatroom);
            if (!list.isEmpty() && num <= 8 && num >= 1) {
                Log.e(TAG, "点击了：" + num);

                final AccessibilityNodeInfo nodeInfo1 = list.get(0).getChild(num - 1);
                Log.e(TAG, "延迟点击");
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        if (nodeInfo1 != null) {
                            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_CLICK);
                        }
                    }
                }, 500);
                return true;
            }
        } catch (Exception e) {
            Log.e(TAG, "异常，未发现按钮");
            return true;
        }

        return false;
    }

    /**
     * T人
     *
     * @param classname
     */
    private void Tnum(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(Tnum), nodeInfo)) {
                    initretdata();
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, Tman)) {
                    //T人失败，这个位置没有人
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            } else if (saveState.equals(page_room_exit_menu_exitdialog)) {
                ClickIdBtn(ExitDialgoRightBtn, nodeInfo);
                initretdata();
            }
        } else {
            reSetWindows(saveState);
        }

    }

    /**
     * 抱人下麦
     *
     * @param classname
     */
    private void Baonum(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(Bnum), nodeInfo)) {
                    initretdata();
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, BaoMan)) {
                    //T人失败，这个位置没有人
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                }
            }
//            else if (saveState.equals(page_room_exit_menu_exitdialog)) {
//                ClickIdBtn(ExitDialgoRightBtn, nodeInfo);
//                initretdata();
//            }
        } else {
            reSetWindows(saveState);
        }

    }


    /**
     * 抱树上麦
     */
    private void BaoShushangMai(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(Tnum), nodeInfo)) {
                    initretdata();
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, Baoshu)) {
                    if (!ClickPopExitButton(nodeInfo, ShangMai)) {
                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                    }
                }
            } else if (saveState.equals(Baosheishangmai)) {
                ClickIdBtn(Btn_SearchUser, nodeInfo);
            } else if (saveState.equals(page_room_serach_dialog)) {
                WriteId(nodeInfo);
                initretdata();
            }
//            else if (saveState.equals(page_room_exit_menu_exitdialog)) {
//                ClickIdBtn(ExitDialgoRightBtn, nodeInfo);
//                initretdata();
//            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 提交抱树ID
     *
     * @param nodeInfo
     */
    private void WriteId(AccessibilityNodeInfo nodeInfo) {
//        作者：俞俞俞其荣
//        链接：https://www.zhihu.com/question/43473655/answer/131252436
//        来源：知乎
//        著作权归作者所有，转载请联系作者获得授权。

//        ndroid 5.0 版本及以上：
        List<AccessibilityNodeInfo> list = null;
        try {
            list = nodeInfo.findAccessibilityNodeInfosByViewId(Edit_SearchUser);
        } catch (Exception e) {
            return;
        }
        if (list == null || list.isEmpty()) {
            return;
        }
        Log.e(TAG, "输入的值:" + SMid);
        AccessibilityNodeInfo nodeInfo1 = list.get(0);

//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Bundle arguments = new Bundle();
//            arguments.putCharSequence(
//                    AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, SMid);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { //        Android 4.3 版本及以上：
//
//            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//            ClipData clip = ClipData.newPlainText("label", SMid);
//            clipboard.setPrimaryClip(clip);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
//        }
        AccessibilityNodeInfoCompat compat;

        Bundle arguments = new Bundle();
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
        arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
                AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE);
        arguments.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
                true);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
                arguments);
        ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("636", SMid);
        clipboard.setPrimaryClip(clip);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);

        ClickIdBtn(Btn_BaoUser, nodeInfo);//修改失败会直接退出
    }

    /**
     * 封闭
     */
    private void Feng(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(FengId), nodeInfo)) {
//                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (ClickPopExitButton(nodeInfo, Baoshu)) {
                    initretdata(); //禁麦成功
//                        mode = -1;
                } else {
                    //禁麦失败，这个位置没有人 或者这个麦已经被禁了
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                        checkMode(saveState);
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 解除封闭座位
     */
    private void Unfeng(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(FengId), nodeInfo)) {
//                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (ClickPopExitButton(nodeInfo, Jiefeng)) {
                    initretdata(); //禁麦成功
//                        mode = -1;
                } else {
                    //禁麦失败，这个位置没有人 或者这个麦已经被禁了
                    initretdata();
                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
//                        checkMode(saveState);
                }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 编辑喇叭
     */
    private void EditLaba(String classname) {
        if (FirstSetState) {
            final AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {

//                if (!ClickNum(Integer.parseInt(FengId), nodeInfo)) {
////                    initretdata();
//                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
////                    checkMode(saveState);
//                }
                if (ClickIdBtn(btn_edit_laba, nodeInfo)) {
//                    new Handler().postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
                    WirteLa(nodeInfo);
//                        }
//                    }, 1000);

                }

//            } else if (saveState.equals(page_room_exit_menu)) {
//                if (ClickPopExitButton(nodeInfo, Jiefeng)) {
//                    initretdata(); //禁麦成功
////                        mode = -1;
//                } else {
//                    //禁麦失败，这个位置没有人 或者这个麦已经被禁了
//                    initretdata();
//                    performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
////                        checkMode(saveState);
//                }
//            }
            }
        } else {
            reSetWindows(saveState);
        }
    }

    /**
     * 修改喇叭
     *
     * @param nodeInfo
     */
    private void WirteLa(AccessibilityNodeInfo nodeInfo) {
//        作者：俞俞俞其荣
//        链接：https://www.zhihu.com/question/43473655/answer/131252436
//        来源：知乎
//        著作权归作者所有，转载请联系作者获得授权。

//        ndroid 5.0 版本及以上：
        List<AccessibilityNodeInfo> list = null;
        try {
            list = nodeInfo.findAccessibilityNodeInfosByViewId(edit_laba);
        } catch (Exception e) {
            return;
        }
        if (list == null || list.isEmpty()) {
            return;
        }
        Log.e(TAG, "输入的值:" + Labainput);
        final AccessibilityNodeInfo nodeInfo1 = list.get(0);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            Bundle arguments = new Bundle();
//            arguments.putCharSequence(
//                    AccessibilityNodeInfo.ACTION_ARGUMENT_SET_TEXT_CHARSEQUENCE, Labainput);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SET_TEXT, arguments);
//        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) { //        Android 4.3 版本及以上：
//
//            ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
//            ClipData clip = ClipData.newPlainText("label", Labainput);
//            clipboard.setPrimaryClip(clip);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
//            nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
//        }
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
        final ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SELECT);
        Log.e(TAG, "选中的开始:" + nodeInfo1.getTextSelectionStart());
        Log.e(TAG, "选中的结束:" + nodeInfo1.getTextSelectionEnd());
        int max = nodeInfo1.getText().toString().length();
        Log.e(TAG, "文本最大长度" + max);
        Bundle arg = new Bundle();
        ClipData clip = ClipData.newPlainText("000", Labainput);
        clipboard.setPrimaryClip(clip);
        arg.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, 0);
        arg.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, max);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, arg);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        ClickIdBtn(Btn_saveLaba, nodeInfo);
        initretdata();
        checkMode(saveState);
//        new Handler().postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                ClipData clip = ClipData.newPlainText("000", Labainput);
//                clipboard.setPrimaryClip(clip);
//                Bundle arguments = new Bundle();
//                arguments.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_MOVEMENT_GRANULARITY_INT,
//                        AccessibilityNodeInfo.MOVEMENT_GRANULARITY_LINE);
//                arguments.putBoolean(AccessibilityNodeInfo.ACTION_ARGUMENT_EXTEND_SELECTION_BOOLEAN,
//                        true);
//                nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PREVIOUS_AT_MOVEMENT_GRANULARITY,
//                        arguments);
//                nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
////            ClickIdBtn(Btn_saveLaba, nodeInfo);
//            }
//
////            initretdata();
////            checkMode(saveState);
//        }, 2000);
    }
//        }, 1000);

//            }
//        }, 5000);


//        nodeInfo1.setText("555");

//        ClickIdBtn(Btn_saveLaba, nodeInfo);//修改失败会直接退出
//        initretdata();
//        checkMode(saveState);


    /**
     * 修改喇叭
     *
     * @param nodeInfo
     */
    private void WriteText(AccessibilityNodeInfo nodeInfo,String textid) {
//        作者：俞俞俞其荣
//        链接：https://www.zhihu.com/question/43473655/answer/131252436
//        来源：知乎
//        著作权归作者所有，转载请联系作者获得授权。

//        ndroid 5.0 版本及以上：
        List<AccessibilityNodeInfo> list = null;
        try {
            list = nodeInfo.findAccessibilityNodeInfosByViewId(textid);
        } catch (Exception e) {
            return;
        }
        if (list == null || list.isEmpty()) {
            return;
        }
        Log.e(TAG, "输入的值:" + Labainput);
        final AccessibilityNodeInfo nodeInfo1 = list.get(0);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_FOCUS);
        final ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SELECT);
        Log.e(TAG, "选中的开始:" + nodeInfo1.getTextSelectionStart());
        Log.e(TAG, "选中的结束:" + nodeInfo1.getTextSelectionEnd());
        int max = nodeInfo1.getText().toString().length();
        Log.e(TAG, "文本最大长度" + max);
        Bundle arg = new Bundle();
        ClipData clip = ClipData.newPlainText("111", Labainput);
        clipboard.setPrimaryClip(clip);
        arg.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_START_INT, 0);
        arg.putInt(AccessibilityNodeInfo.ACTION_ARGUMENT_SELECTION_END_INT, max);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_SET_SELECTION, arg);
        nodeInfo1.performAction(AccessibilityNodeInfo.ACTION_PASTE);
        ClickIdBtn(Btn_saveLaba, nodeInfo);
        initretdata();
        checkMode(saveState);
    }

    /**
     * 抱踢上麦
     */

    private void BaoTi(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickNum(Integer.parseInt(Tnum), nodeInfo)) {
                    initretdata();
//                    checkMode(saveState);
                }
            } else if (saveState.equals(page_room_exit_menu)) {
                if (!ClickPopExitButton(nodeInfo, Baoshu)) {
                    if (!ClickPopExitButton(nodeInfo, ShangMai)) {
                        performGlobalAction(AccessibilityService.GLOBAL_ACTION_BACK);
                    }
                }
            } else if (saveState.equals(Baosheishangmai)) {
                ClickIdBtn(Btn_SearchUser, nodeInfo);
            } else if (saveState.equals(page_room_serach_dialog)) {
                WriteId(nodeInfo);
                mode = 10;
                tmpdata = Tnum;
                FirstSetState = false;
            }
//            else if (saveState.equals(page_room_exit_menu_exitdialog)) {
//                ClickIdBtn(ExitDialgoRightBtn, nodeInfo);
//                initretdata();
//            }
        } else {
            reSetWindows(saveState);
        }
    }

    public void GetListT(String classname) {
        if (FirstSetState) {
            AccessibilityNodeInfo nodeInfo = getRootInActiveWindow();
            if (saveState.equals(page_room)) {
                if (!ClickIdBtn(btn_enter_top, nodeInfo)) {
                    initretdata();
//                    checkMode(saveState);
                }
            } else if (saveState.equals(Baosheishangmai)) {
                List<AccessibilityNodeInfo> list = nodeInfo.findAccessibilityNodeInfosByViewId("android:id/list");
                boolean findname = true;
                AccessibilityNodeInfo lastone = null;
                while (findname) {
                    Log.e(TAG, "list长度：" + list.get(0).getChildCount());
                    Log.e(TAG, "list可否滑动：" + list.get(0).isScrollable());
                    Log.e(TAG, "tanme:" + Tname);
                    if (lastone != null) {
                        if (lastone.equals(list.get(0).getChild(list.get(0).getChildCount() - 1))) {
                            Log.e(TAG, "跑到最后一步啦");
                            initretdata();
                            checkMode(saveState);
                            break;
                        }
                    }
                    if (list.get(0).getChildCount() != 0) {

                        List<AccessibilityNodeInfo> list2 = list.get(0).findAccessibilityNodeInfosByText(Tname);
                        if (list2 != null && list2.size() != 0) {
                            for (AccessibilityNodeInfo no : list2) {
                                if ((no.getText().toString()).equals(Tname)) {
                                    //符合T人的名字 就T掉
                                    List<AccessibilityNodeInfo> list3 = no.getParent().getParent().findAccessibilityNodeInfosByViewId(btn_kicout);
                                    if (list3 != null && list3.size() != 0) //如果有这个按钮就T掉
                                    {
                                        list3.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
                                        findname = false;
                                        break;
                                    }
                                }
                            }
                        }
                        if (!findname) {
                            break;
                        }
                        if (!list.get(0).isScrollable()) {
                            Log.e(TAG, "列表只有一页啦");
                            initretdata();
                            checkMode(saveState);
                            break;
                        } else {
                            lastone = list.get(0).getChild(list.get(0).getChildCount() - 1);
                            list.get(0).performAction(AccessibilityNodeInfo.ACTION_SCROLL_FORWARD);
                        }
                    } else {
                        //没有数据了
                        Log.e(TAG, "没有数据拉");
                        initretdata();
                        checkMode(saveState);
                        break;
                    }
                }
            } else if (saveState.equals(page_room_exit_menu_exitdialog)) {
                ClickIdBtn(ExitDialgoRightBtn, nodeInfo);
                initretdata();
            }
        } else {
            reSetWindows(saveState);
        }
    }


    private void GetPwd() {
        String pwd = (String) SPUtils.get(getApplication(), "password", "123");
        if (pwd != null) {
            pwds = pwd;
        }
    }

    private static final String ACTION_TIME_CHANGED = Intent.ACTION_TIME_CHANGED;

    public class TimeReciver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

        }
    }
}
